<%@page import="java.util.prefs.Preferences"%>
<%@page import="java.util.Map"%>
<%@page import="org.restql.webapp.RestQLServlet"%>
<%@page import="java.util.Date" %>
<%
	Map params = request.getParameterMap();
	String configTypeArray[] = (String []) params.get("configType");
	Preferences userPrefs = Preferences.userNodeForPackage(RestQLServlet.class);
	
	if (configTypeArray != null) {
		userPrefs.put("configType", configTypeArray[0]);
		if (params.containsKey("configLocation"))
			userPrefs.put("configLocation", ((String []) params.get("configLocation"))[0]);
		if (params.containsKey("dbtype"))
			userPrefs.put("dbtype", ((String []) params.get("dbtype"))[0]);
		
		if (params.containsKey("allowGet"))
			userPrefs.put("allowGet", ((String []) params.get("allowGet"))[0]);
		else
			userPrefs.put("allowGet", "false");
		if (params.containsKey("allowPost"))
			userPrefs.put("allowPost", ((String []) params.get("allowPost"))[0]);
		else
			userPrefs.put("allowPost", "false");
		if (params.containsKey("allowPut"))
			userPrefs.put("allowPut", ((String []) params.get("allowPut"))[0]);
		else
			userPrefs.put("allowPut", "false");
		if (params.containsKey("allowDelete"))
			userPrefs.put("allowDelete", ((String []) params.get("allowDelete"))[0]);
		else
			userPrefs.put("allowDelete", "false");

		RestQLServlet.applyConfigToAll();
	} 
	String configLocation = userPrefs.get("configLocation", "");
	String configType = userPrefs.get("configType", "auto");
	String dbtype = userPrefs.get("dbtype", "mysql");
	String allowGet = userPrefs.get("allowGet", "true");
	String allowPost = userPrefs.get("allowPost", "false");
	String allowPut = userPrefs.get("allowPut", "false");
	String allowDelete = userPrefs.get("allowDelete", "false");
%>
<html>

	<head>
		<title>RestQL</title>
		<style>
			h1 {
				color: grey;
				font-family: Verdana, Arial;
				font-size: 36px;
			}
			h2 {
				color: grey;
				font-family: Verdana;
				font-size: 24px;
			}
			h3 {
				color: grey;
				font-family: Verdana;
				font-size: 16px;
			}
			body {
				margin: 0px;
				padding: 12px;
				border: solid 12px grey;
				font-family: Verdana, Arial;
				font-size: 14px;
			}
			pre {
				margin-left: 24px;
			}
			.textfield {
				width: 40em;
			}
			table {
				border: 2px solid grey;
				margin: 2px;
				width: 50em;
			}
		</style>
	</head>
	<body>
		<h1>restQL</h1>
		<h2>Configuration</h2>
		<form action='config.jsp' method='POST'>
			<table>
				<tr>
					<td colspan='2'>
						<input type='radio' name='configType' value='file' 
						<% if (configType.equals("file")) { %>
						checked
						<% } %>
						>Use External File
					</td>
				</tr>
				<tr>
					<td>Config File Location</td>
					<td><input class='textfield' type='text' name='configLocation' 
					   value='<%= configLocation %>'></td>
				</tr>
			</table>
			<table>
				<tr>
					<td colspan='2'>
						<input type='radio' name='configType' value='auto' 
						<% if (configType.equals("auto")) { %>
						checked
						<% } %> />Auto Config
					</td>
				</tr>
				<tr>
					<td>Database Type</td>
					<td>
						<select name='dbtype'>
							<option value='mysql'>MySQL</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>Allowed Operations</td>
					<td>
						<input type='checkbox' name='allowGet' value='true' 
						<% if (allowGet.equals("true")) { %>
						checked
						<% } %> />GET (select)<br/>
						<input type='checkbox' name='allowPost' value='true' 
						<% if (allowPost.equals("true")) { %>
						checked
						<% } %>/>POST (update)<br/>
						<input type='checkbox' name='allowPut' value='true' 
						<% if (allowPut.equals("true")) { %>
						checked
						<% } %>/>PUT (insert)<br/>
						<input type='checkbox' name='allowDelete' value='true' 
						<% if (allowDelete.equals("true")) { %>
						checked
						<% } %>/>DELETE (delete)<br/>
					</td>
				</tr>
			</table>
			<input type='submit' value='Save' />
		</form>

		<% if (configTypeArray != null) { %>
			<font color='red'>Saved settings: <%= new Date().toString() %></font>
		<% } %>
	</body>
</html>